import store from '@/store';

function checkPermission(el, binding) {
    const { value } = binding // value=> ['admins', 'editors'] 
    console.log(el,value,' ==============1234567 ') 
    if (value && value instanceof Array && value.length > 0) {
        console.log(`store.getters.roles ================== `)
        console.log(store.getters.userRoles)
        let storeRoles = (store.getters && store.getters.userRoles) || [];
        let hasPermission = storeRoles.some(item => {
            return value.includes(item);
        })
        // 没有权限，则把元素删除掉
        if (!hasPermission) {
            el.parentNode && el.parentNode.removeChild(el)
        }
    } else {
        console.error(`need roles! Like v-permission="['admin','editor']"`)
        return false
    }

}

export default {
    inserted(el, binding) {
        checkPermission(el, binding)
    },
    update(el, binding) {
        checkPermission(el, binding)
    }
} 
